﻿// -----------------------------------------------------------------------
// <copyright file="ProfileTypeProgressData.cs" company="VDC-IT" author="LTDUC">
//      Copyright (c) VDC-IT. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace VDC.VONE.DAL
{
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Reflection;
    using Microsoft.ApplicationBlocks.Data;
    using VDC.COMMON;
    using VDC.VONE.ENTITIES;
    
    /// <summary>
    /// Class access to db ProfileTypeProgress
    /// </summary>
    public class ProfileTypeProgressData
    {
        /// <summary>
        /// Get ProfileTypeProgress By Id
        /// </summary>
        /// <param name="profileTypeProgressId">Param profileTypeProgressId</param>
        /// <returns>Return ProfileTypeProgress</returns>
        public ProfileTypeProgressEN GetProfileTypeProgressById(int profileTypeProgressId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                ProfileTypeProgressEN profileTypeProgress = null;
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeProgressGetById, profileTypeProgressId);
                while (reader.Read())
                {
                    profileTypeProgress = new ProfileTypeProgressEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileTypeProgress.ProfileTypeProgressId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeProgress.ProgressId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileTypeProgress.ProfileTypeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        profileTypeProgress.DecideDays = reader.GetInt32(3);
                    }
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return profileTypeProgress;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get list all ProfileTypeProgress
        /// </summary>
        /// <returns>Return list all ProfileTypeProgress</returns>
        public IList<ProfileTypeProgressEN> GetProfileTypeProgress()
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileTypeProgressEN> lstProfileTypeProgress = new List<ProfileTypeProgressEN>();
                SqlDataReader reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeProgressGetAll);
                while (reader.Read())
                {
                    ProfileTypeProgressEN profileTypeProgress = new ProfileTypeProgressEN();

                    if (!reader.IsDBNull(0))
                    {
                        profileTypeProgress.ProfileTypeProgressId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeProgress.ProgressId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileTypeProgress.ProfileTypeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        profileTypeProgress.DecideDays = reader.GetInt32(3);
                    }

                    lstProfileTypeProgress.Add(profileTypeProgress);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileTypeProgress;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Insert to db ProfileTypeProgress
        /// </summary>
        /// <param name="profileTypeProgress">Param ProfileTypeProgress</param>
        /// <returns>Return Fail: 0, Success: @@Identity</returns>
        public int Insert(ProfileTypeProgressEN profileTypeProgress)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeProgress.ProgressId, profileTypeProgress.ProgressId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeProgress.ProfileTypeId, profileTypeProgress.ProfileTypeId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeProgress.DecideDays, profileTypeProgress.DecideDays)
                                };

                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeProgressInsert, sqlParameters));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Update to db ProfileTypeProgress
        /// </summary>
        /// <param name="profileTypeProgress">Param ProfileTypeProgress</param>
        public void Update(ProfileTypeProgressEN profileTypeProgress)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[]
                                {
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeProgress.ProfileTypeProgressId, profileTypeProgress.ProfileTypeProgressId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeProgress.ProgressId, profileTypeProgress.ProgressId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeProgress.ProfileTypeId, profileTypeProgress.ProfileTypeId),
                                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeProgress.DecideDays, profileTypeProgress.DecideDays)
                                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeProgressUpdate, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Delete ProfileTypeProgress by profileTypeProgressId
        /// </summary>
        /// <param name="profileTypeProgressId">param profileTypeProgressId</param>
        public void Delete(int profileTypeProgressId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var sqlParameters = new[] 
                { 
                    new SqlParameter(Constants.SqlParamerterName.ProfileTypeProgress.ProfileTypeProgressId, profileTypeProgressId)
                };

                SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeProgressDelete, sqlParameters);
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List VOneProfileTypeProgress Get By ProgressId
        /// </summary>
        /// <param name="progressId">Parm progressId</param>
        /// <returns>Return List ProfileTypeProgress</returns>
        public IList<ProfileTypeProgressEN> GetProfileTypeProgressByProgressId(int progressId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileTypeProgressEN> lstProfileTypeProgress = new List<ProfileTypeProgressEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeProgressGetByProgressId, progressId);
                while (reader.Read())
                {
                    ProfileTypeProgressEN profileTypeProgress = new ProfileTypeProgressEN();
                    if (!reader.IsDBNull(0))
                    {
                        profileTypeProgress.ProfileTypeProgressId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeProgress.ProgressId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileTypeProgress.ProfileTypeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        profileTypeProgress.DecideDays = reader.GetInt32(3);
                    }

                    lstProfileTypeProgress.Add(profileTypeProgress);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileTypeProgress;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(ex);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Get List ProfileTypeProgress Get ProfileTypeId
        /// </summary>
        /// <param name="profileTypeId">Param ProfileTypeId</param>
        /// <returns>Return List ProfileTypeProgress</returns>
        public IList<ProfileTypeProgressEN> GetProfileTypeProgressByProfileTypeId(int profileTypeId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                IList<ProfileTypeProgressEN> lstProfileTypeProgress = new List<ProfileTypeProgressEN>();
                var reader = SqlHelper.ExecuteReader(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeProgressGetByProfileTypeId, profileTypeId);
                while (reader.Read())
                {
                    ProfileTypeProgressEN profileTypeProgress = new ProfileTypeProgressEN();
                    if (!reader.IsDBNull(0))
                    {
                        profileTypeProgress.ProfileTypeProgressId = reader.GetInt32(0);
                    }

                    if (!reader.IsDBNull(1))
                    {
                        profileTypeProgress.ProgressId = reader.GetInt32(1);
                    }

                    if (!reader.IsDBNull(2))
                    {
                        profileTypeProgress.ProfileTypeId = reader.GetInt32(2);
                    }

                    if (!reader.IsDBNull(3))
                    {
                        profileTypeProgress.DecideDays = reader.GetInt32(3);
                    }

                    lstProfileTypeProgress.Add(profileTypeProgress);
                }

                reader.Close();
                reader.Dispose();
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return lstProfileTypeProgress;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(ex);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist code
        /// </summary>
        /// <param name="profileTypeProgressId">Param profileTypeProgressId</param>
        /// <param name="profileTypeId">Param profileTypeId</param>
        /// <param name="progressId">Param progressId</param>
        /// <returns>positionData.Exist(profileTypeId, progressId)</returns>
        public bool Exist(int profileTypeProgressId, int profileTypeId, int progressId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeProgressExist, profileTypeProgressId, profileTypeId, progressId));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }

        /// <summary>
        /// Exist profiletypeprogressId in workflow
        /// </summary>
        /// <param name="profileTypeProgressId">Param profileTypeProgressId</param>
        /// <returns>return true/false</returns>
        public bool Exist(int profileTypeProgressId)
        {
            string methodName = MethodBase.GetCurrentMethod().DeclaringType.ToString() + Constants.Dot + MethodBase.GetCurrentMethod().Name;
            LogHelper.Instance.WriteInfo(Constants.Begin + methodName);
            try
            {
                var result = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, Constants.StorepProcedure.VOneProfileTypeProgressExistInWorkflow, profileTypeProgressId));
                LogHelper.Instance.WriteInfo(Constants.End + methodName);
                return result > 0;
            }
            catch (SqlException ex)
            {
                LogHelper.Instance.WriteError(methodName + Constants.Colon + ex.Message);
                throw new Exception(ex.ToString());
            }
        }
    }
}